﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CNVP.JShop.UI;
using CNVP.Framework;
using CNVP.Framework.Utils;

namespace CNVP.JShop.Web.Admin
{
    public partial class OrderEdit : AdminPage
    {
        BLL.CNVP_Order bll = new BLL.CNVP_Order();
        Model.CNVP_Order model = new Model.CNVP_Order();
        public int id = 0;
        public string Province = "0", City = "0", Area = "0";
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!BLL.BLLCheckAdmin.LoginOk(Page, 94)) return;//判断页面权限
            if (!string.IsNullOrEmpty(Request.QueryString["ID"]) && Public.IsNumber(Request.QueryString["ID"]))
            {
                id = Convert.ToInt32(Request.QueryString["ID"]);
            }
            if (!IsPostBack)
            {
                ddlBind();
                ProvinceBind();
                OrderProList();

                #region 验证
                txtDeliveryPrice.Attributes.Add("onchange", "TotalCount()");
                txtPayFactorage.Attributes.Add("onchange", "TotalCount()");
                txtSalesPromotionAmount.Attributes.Add("onchange", "TotalCount()");
                txtDiscountOrder.Attributes.Add("onchange", "TotalCount()");
                txtSupportValuePrice.Attributes.Add("onchange", "TotalCount()");
                cbIsSupportValue.Attributes.Add("onclick", "TotalCount()");
                txtBilTaxes.Attributes.Add("onchange", "TotalCount()");
                cbIsNeedBill.Attributes.Add("onclick", "TotalCount()");
                ddlProvince.Attributes.Add("onchange", "ProvinceChange()");
                #endregion
            }
        }

        private void ProvinceBind()
        {
            IList<Model.CNVP_Province> model = new BLL.CNVP_Province().GetAll_CNVP_Province();
            foreach (Model.CNVP_Province p in model)
            {
                ddlProvince.Items.Add(new ListItem(p.Name, p.Code));
            }
        }
        //支付方式
        private void ddlBind()
        {
            #region 支付方式
            IList<Model.CNVP_Column> model_PayWay = new BLL.CNVP_Column().GetAll_CNVP_Column("ColumnType in (5,7)");
            foreach (Model.CNVP_Column D in model_PayWay)
            {
                ddlPayWay.Items.Add(new ListItem(D.ColumnName, D.ID.ToString()));
            }
            #endregion
        }

        private void OrderProList()
        {
            #region 商品信息
            var AddTr = "";
            AddTr += "<table id=\"tableEdit\">";
            AddTr += "	<tr >";
            AddTr += "		<th  style=\"text-align:left;width:100px;\" >";
            AddTr += "		货号</th>";
            AddTr += "		<th style=\"text-align:left;width:200px;\">";
            AddTr += "		商品名称</th>";
            AddTr += "		<th style=\"text-align:left;width:36px;\">";
            AddTr += "		价格</th>";
            AddTr += "		<th style=\"text-align:left;width:36px;\">";
            AddTr += "		购买数量</th>";
            AddTr += "		<th style=\"text-align:left;width:36px;\">";
            AddTr += "		小计</th>";
            AddTr += "		<th >";
            AddTr += "		</th>";
            AddTr += "	</tr>";
            string html = "";
            var list_OrderProducts = new BLL.CNVP_OrderProducts().GetAll_CNVP_OrderProducts(id);
            for (int i = 0; i < list_OrderProducts.Count; i++)
            {
                html += "	<tr id=\"tr" + list_OrderProducts[i].ID + "\" >";
                html += "		<td >";
                html += "		" + list_OrderProducts[i].ProductNo + "</td>";
                html += "		<td  style=\"width:300px;\" >";
                html += "                  <a href=\"/ProductDetail_" + list_OrderProducts[i].ProductClassID + "_" + list_OrderProducts[i].ProductID + ".aspx\" target=\"_blank\" title=\"" + list_OrderProducts[i].ProductName + "\">";
                html += "		" + list_OrderProducts[i].ProductName + "";
                html += "                  </a>";
                html += "		</td>";
                html += "		<td >";
                string IDS = list_OrderProducts[i].ProductID + "_" + list_OrderProducts[i].ProductsDetailID;
                html += "		<input type=\"text\" id=\"Price" + list_OrderProducts[i].ID + "\" name=\"Price\" value='" + list_OrderProducts[i].Price + "' onchange=\"OrderProductsOnchang(" + list_OrderProducts[i].ID + ",'" + IDS + "');\" maxlength=\"40\" style=\"width:40px;\"></td>";
                html += "		<td >";
                html += "		<input type=\"text\" id=\"Num" + list_OrderProducts[i].ID + "\" name=\"Num\" value='" + list_OrderProducts[i].Num + "'  onchange=\"OrderProductsOnchang(" + list_OrderProducts[i].ID + ",'" + IDS + "');\" maxlength=\"40\" style=\"width:40px;\">";
                html += "		<input type=\"text\" id=\"NumCompare" + list_OrderProducts[i].ID + "\" name=\"NumCompare\" value='" + list_OrderProducts[i].Num + "' maxlength=\"40\" style=\"width:40px;display:none;\"></td>";
                html += "		<td >";
                html += "		<span id=\"spantotalPrice" + list_OrderProducts[i].ID + "\">" + list_OrderProducts[i].TotalPrice + "</span>";
                html += "       <input type=\"text\" id=\"totalPrice" + list_OrderProducts[i].ID + "\" name=\"totalPrice\" value=\"" + list_OrderProducts[i].TotalPrice + "\" class=\"Display\" /></td>";
                html += "		<td >";
                html += "		<input id=\"BTDel\" type=\"button\" value=\"删除\" onclick=\"Del(" + list_OrderProducts[i].ID + ")\" />";
                html += "       <input id=\"CKOrderProducts\" name=\"CKOrderProducts\" type=\"checkbox\" checked=\"checked\" class =\"Display\" value=\"" + list_OrderProducts[i].ID + "\" />";
                html += "	    </td>";
                html += "	</tr>";
            }
            AddTr += html;
            AddTr += "</table>";
            lbOrderProducts.Text = AddTr;
            #endregion

            #region 订单信息
            model =new BLL.CNVP_Order().GetSingle_CNVP_OrderById(id);
            if (model != null)
            {
                lbOrderNum.Text = model.OrderNum.ToString();
                lbAddTime.Text = model.AddTime.ToString();
                txtProductsTotalAmount.Text = model.ProductsTotalAmount.ToString();
                txtDeliveryPrice.Text = model.DeliveryPrice.ToString();
                txtSupportValuePrice.Text = model.SupportValuePrice.ToString();
                if (model.IsSupportValue == 1)
                {
                    cbIsSupportValue.Checked = true;
                }
                else
                {
                    cbIsSupportValue.Checked = false;
                }
                txtProductWeight.Text = model.ProductWeight.ToString();
                txtPayFactorage.Text = model.PayFactorage.ToString();
                txtBillRises.Text = model.BillRises.ToString();
                txtBilTaxes.Text = model.BilTaxes.ToString();
                txtSalesPromotionAmount.Text = model.SalesPromotionAmount.ToString();
                txtDiscountOrder.Text = model.DiscountOrder.ToString();
                txtOrderTotalAmount.Text = model.OrderTotalAmount.ToString();
                txtGetTrueName.Text = model.GetTrueName;
                txtMobilePhone.Text = model.MobilePhone;
                txtPhone.Text = model.Phone;
                txtEmail.Text = model.Email;
                txtPostTime.Value = model.PostTime.ToString("yyyy-MM-dd");
                txtZipCode.Text = model.ZipCode;
                ddlProvince.SelectedValue = model.Province;
                Province = model.Province;
                City = model.City;
                Area = model.Area;
                txtGetAddress.Text = model.GetAddress;
                if (model.IsNeedBill == 1)
                {
                    cbIsNeedBill.Checked = true;
                }
                else
                {
                    cbIsNeedBill.Checked = false;
                }
                ViewState["DeliveryWay"] = model.DeliveryWay;
                lbDeliveryWay.Text = model.DeliveryWay;
                ddlPayWay.SelectedValue = model.PayWay;
                txtText.Text = model.Text;
            }
            #endregion

            #region 购买人信息
            var user_model = new BLL.CNVP_Users().GetSingle_CNVP_UsersById(model.BuyerID);
            if (user_model != null)
            {
                AddTr = "";
                AddTr += " <table border=\"0\" cellpadding=\"0\" cellspacing=\"0\" width=\"100%\">";
                AddTr += "        <tbody >";
                AddTr += "            <tr >";
                AddTr += "                <th>";
                AddTr += "                    姓&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;名：</th>";
                AddTr += "                <td>";
                AddTr += "                " + user_model.UserName + "</td>";
                AddTr += "                <th>";
                AddTr += "                     Email地址：</th>";
                AddTr += "                <td>";
                AddTr += "                    " + user_model.Email + "</td>";
                AddTr += "            </tr>";
                AddTr += "            <tr >";
                AddTr += "                <th>";
                AddTr += "                    会员级别：</th>";
                AddTr += "                <td>";
                var model_UserLevel=new BLL.CNVP_UserLevel().GetSingle_CNVP_UserLevelById(user_model.UserLevelID);
                if (model_UserLevel != null)
                {
                    AddTr += "                 " + model_UserLevel.LevelName + "";
                }
                AddTr += "                </td>";
                AddTr += "                <th>";
                AddTr += "                   </th>";
                AddTr += "                <td>";
                AddTr += "                     </td>";
                AddTr += "            </tr>";
                AddTr += "            <tr >";
                AddTr += "                <th>";
                AddTr += "                    当前积分：</th>";
                AddTr += "                <td>";
                AddTr += "                 " + user_model.PointsNum + "</td>";
                AddTr += "                <th>";
                AddTr += "                    注册时间：</th>";
                AddTr += "                <td>";
                AddTr += "                " + user_model.AddTime + "</td>";
                AddTr += "            </tr>";
                AddTr += "            <tr >";
                AddTr += "                <th>";
                AddTr += "                    最后登录时间：</th>";
                AddTr += "                <td>";
                AddTr += "                " + user_model.EndLoginTime + "</td>";
                AddTr += "                <th>";
                AddTr += "                 最后登录IP</th>";
                AddTr += "                <td>";
                AddTr += "                " + user_model.EndLoginIP + "</td>";
                AddTr += "            </tr>";
                AddTr += "        </tbody>";
                AddTr += "    </table>";
                lbBuyerInfo.Text = AddTr;
            }
            #endregion

            
        }

        protected void btUp_Click(object sender, EventArgs e)
        {
            if (id > 0)
            {
                try
                {
                    decimal Price_Decimal = 0;
                    int Num_Int = 0;
                    decimal TotalPrice_Decimal = 0;
                    string _CKOrderProducts=Request.Form["CKOrderProducts"];
                    if (!string.IsNullOrEmpty(_CKOrderProducts))
                    {
                        string[] CKOrderProducts = _CKOrderProducts.Split(Convert.ToChar(","));
                        for (int i = 0; i < CKOrderProducts.Length; i++)
                        {
                            string[] PriceValue = Request.Form["Price"].Split(Convert.ToChar(","));
                            try
                            {
                                Price_Decimal = decimal.Parse(PriceValue[i]);
                            }
                            catch { Price_Decimal = 0; }
                            string[] NumValue = Request.Form["Num"].Split(Convert.ToChar(","));
                            try
                            {
                                Num_Int = int.Parse(NumValue[i]);
                            }
                            catch { Num_Int = 0; }
                            string[] TotalPriceValue = Request.Form["TotalPrice"].Split(Convert.ToChar(","));
                            try
                            {
                                TotalPrice_Decimal = decimal.Parse(TotalPriceValue[i]);
                            }
                            catch { TotalPrice_Decimal = 0; }
                            int rows_OrderProducts = new BLL.CNVP_OrderProducts().Update_CNVP_OrderProducts(new Model.CNVP_OrderProducts()
                            {
                                ID = int.Parse(CKOrderProducts[i]),
                                Price = Price_Decimal,
                                Num = Num_Int,
                                TotalPrice = TotalPrice_Decimal
                            });
                        }
                    }
                    int rows = new BLL.CNVP_Order().Update_CNVP_Order(new Model.CNVP_Order()
                    {
                        ID = id,
                        OrderNum = lbOrderNum.Text,
                        ProductsTotalAmount = decimal.Parse(Request.Form["txtProductsTotalAmount"]),
                        DeliveryPrice = decimal.Parse(txtDeliveryPrice.Text),
                        SupportValuePrice = decimal.Parse(txtSupportValuePrice.Text),
                        IsSupportValue = cbIsSupportValue.Checked == true ? (byte)1 : (byte)0,
                        ProductWeight = int.Parse(txtProductWeight.Text),
                        PayFactorage = decimal.Parse(txtPayFactorage.Text),
                        BillRises = txtBillRises.Text,
                        BilTaxes = decimal.Parse(txtBilTaxes.Text),
                        SalesPromotionAmount = decimal.Parse(txtSalesPromotionAmount.Text),
                        DiscountOrder = decimal.Parse(txtDiscountOrder.Text),
                        OrderTotalAmount = decimal.Parse(Request.Form["txtOrderTotalAmount"]),
                        GetTrueName = txtGetTrueName.Text,
                        MobilePhone = txtMobilePhone.Text,
                        Phone = txtPhone.Text,
                        Email = txtEmail.Text,
                        PostTime = Convert.ToDateTime(Request.Form["txtPostTime"]),
                        ZipCode = txtZipCode.Text,
                        GetAddress = txtGetAddress.Text,
                        IsNeedBill = cbIsNeedBill.Checked == true ? (byte)1 : (byte)0,
                        Province=ddlProvince.SelectedValue,
                        City = Request.Form["ListCity"],
                        Area = Request.Form["ListArea"],
                        DeliveryWay = Convert.ToString(ViewState["DeliveryWay"]),
                        PayWay=ddlPayWay.SelectedValue,
                        Text=txtText.Text.Trim()
                    });
                    string txtindexNum_Str = txtindexNum.Value;
                    if (!string.IsNullOrEmpty(txtindexNum_Str))
                    {
                        txtindexNum_Str = txtindexNum_Str.Substring(0, txtindexNum_Str.Length - 1);
                        string[] DelArray = txtindexNum_Str.Split(Convert.ToChar(","));
                        if (DelArray.Length > 0)
                        {
                            for (int i = 0; i < DelArray.Length; i++)
                            {
                                new BLL.CNVP_OrderProducts().Delete_CNVP_OrderProducts(int.Parse(DelArray[i]));
                            }
                        }
                    }

                    OrderProList();
                    BasePage.MessageBox("修改成功！", "OrderList.aspx?olbid=" + Request.QueryString["olbid"] + "&Page=" + Request.QueryString["Page"] + "");
                }
                catch
                {
                    BasePage.MessageBoxGoHistory("修改失败！");
                }
            }
        }
    }
}